package com.example.music.ui.music;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.navigation.Navigation;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.music.MainActivity;
import com.example.music.R;
import com.example.music.adapter.MusicsAdapter;
import com.example.music.databinding.FragmentMusicBinding;
import com.example.music.entity.DataMusic;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;


public class MusicFragment extends Fragment {

    private MusicViewModel notificationsViewModel;
    private FragmentMusicBinding binding;
    private List<DataMusic> dataMusics = new ArrayList<>();

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        notificationsViewModel =
                new ViewModelProvider(this).get(MusicViewModel.class);

        binding = FragmentMusicBinding.inflate(inflater, container, false);
        binding.musicGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                String text = ((RadioButton) binding.getRoot().findViewById(checkedId)).getText().toString().trim();
                notificationsViewModel.setFilter(text);
            }
        });
        MusicsAdapter musicsAdapter = new MusicsAdapter(dataMusics, getContext(), new MusicsAdapter.Event() {
            @Override
            public void click(DataMusic dataMusic) {
                Bundle bundle = new Bundle();
                bundle.putSerializable("musics",dataMusic);
                Navigation.findNavController(binding.getRoot()).navigate(R.id.action_navigation_music_to_navigation_play,bundle);

            }
        });
        binding.rv.setAdapter(musicsAdapter);
        binding.rv.setLayoutManager(new GridLayoutManager(getContext(),3));
        notificationsViewModel.getText().observe(getViewLifecycleOwner(), new Observer<List<DataMusic>>() {
            @Override
            public void onChanged(List<DataMusic> dataMusic) {
                if (dataMusic == null || dataMusic.size()<1){
                    binding.gone.setVisibility(View.VISIBLE);
                }else {
                    binding.gone.setVisibility(View.GONE);
                }
                dataMusics.clear();
                if (dataMusic != null) {
                    dataMusics.addAll(dataMusic);
                }
                musicsAdapter.notifyDataSetChanged();
            }
        });
        ((MainActivity) requireActivity()).visibleAll();
        return binding.getRoot();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }
}